package com.example.tao_city.text1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by TAO_CITY on 2018/1/5.
 */

public class SelectNullAction extends Activity implements View.OnClickListener{
    private ImageView nback;
    private EditText neditText;
    private ImageView ndelete;
    private ImageView nsearch;
    public static String ntext;

    @Override
    protected void onCreate(Bundle save){
        super.onCreate(save);
        setContentView(R.layout.select_null);
        initbutton();

    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.select_null_back:
                if(!(String.valueOf(neditText.getText()).equals(""))) {
                    Intent intent = new Intent();
                    intent.putExtra("nte", false);
                    this.setResult(10, intent);
                } else{
                    Intent intent = new Intent();
                    intent.putExtra("nte", true);
                    this.setResult(10, intent);
                }
                finish();
                break;
            case R.id.select_null_delet:
                ndelete.setVisibility(View.INVISIBLE);
                neditText.setText("");
                break;
            case R.id.select_null_search:
                if (!(String.valueOf(neditText.getText()).equals(""))) {
                    Toast.makeText(this,"您搜索了："+neditText.getText(),Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"搜索为空请重新输入",Toast.LENGTH_SHORT).show();
                }
                SelectNullAction.ntext = String.valueOf(neditText.getText());
                break;
        }

    }
    private void initbutton(){
        nback=(ImageView)findViewById(R.id.select_null_back);
        neditText=(EditText)findViewById(R.id.select_null_edittext);
        ndelete=(ImageView)findViewById(R.id.select_null_delet);
        nsearch=(ImageView)findViewById(R.id.select_null_search);

        nback.setOnClickListener(this);
        nsearch.setOnClickListener(this);
        ndelete.setOnClickListener(this);
        //当文本发生变化时调用此方法
        neditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            //  s:textview中显示的文本； start:修改后的文本的起始范围的偏移量； before:替换的旧文本的长度；count:替换的修改文本的长度。
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(start==0 ){
                    ndelete.setVisibility(View.INVISIBLE);
                }else {
                    ndelete.setVisibility(View.VISIBLE);
                }
                if(count!=0){
                    ndelete.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    public boolean onKeyDown(int keyCode, KeyEvent event){//物理返回按钮监听
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {

            if(!(String.valueOf(neditText.getText()).equals(""))) {
                Intent intent = new Intent();
                intent.putExtra("nte", false);
                this.setResult(10, intent);
            } else{
                Intent intent = new Intent();
                intent.putExtra("nte", true);
                this.setResult(10, intent);
            }
            finish();
        }
        return false;
    }
}
